//
//  PlayMusicViewController.m
//  Travel
//
//  Created by LeeZhe on 15/4/13.
//  Copyright (c) 2015年 &#21556;&#21697;&#27901;. All rights reserved.
//

#import "PlayMusicViewController.h"
#import <UIImageView+WebCache.h>
#import "HotThird.h"
#import "PCSEQVisualizer.h"
#import "AudioPlayer.h"
#import <AudioStreamer.h>
@interface PlayMusicViewController ()
@property (nonatomic, retain) UILabel *positionLabel2;
@property (nonatomic, retain) UILabel *positionLabel;

@property (nonatomic,retain)UIView *naviView;
@property (nonatomic,retain)UIButton *backButton;
@property (nonatomic,retain)UILabel *titleOne;

@property (nonatomic, retain) UISlider *volumeViewSlider;

@property (nonatomic,retain)UIImageView *centerView;
@property (nonatomic,retain)UIImageView *backView;
@property (nonatomic,retain)UIImageView *playView;
@property (nonatomic,retain)UIImageView *shareView;
@property (nonatomic,retain)UIImageView *nextSong;
@property (nonatomic,retain)UIImageView *frontSong;
@property (nonatomic,retain)UIImageView *nextView;
@property (nonatomic,retain)UIView  *totalView;
@property (nonatomic, retain) NSString *escapedValue;
@property (nonatomic, retain)  AudioStreamer *streamer;
@property (nonatomic, retain) UISlider *progressSlider;


@property (nonatomic, retain) UIAlertView *alertView;

@property (nonatomic,retain)UIProgressView *progressView;
@property (nonatomic,retain)UIButton *progressButton;
@property (nonatomic,retain)UIImageView *someView;
@property (nonatomic,retain)HotThird *music;
@property (nonatomic,assign)BOOL flag;
@property (nonatomic, retain) NSTimer *progressUpdateTimer;
@property (nonatomic, retain) PCSEQVisualizer *eq;


@end

@implementation PlayMusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // self.View 的底层
    self.someView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [_someView setImage:[UIImage imageNamed:@"播放背景.jpg"]];
    [self createView];
    [self createAnimation];
    
    
    [self.view addSubview:_someView];
    [self cratePlayView];
    
    [self CreatedownloadSourceField];  // 创建连接
    [self toopImage];
    [self createStreamer];  // 创建音频对象;
    [self CreatePositionLabel];
    [self CreateProgressSlider];
    [self sliderMoved:_progressSlider];
    [self Monitor:_progressSlider];
    [self setImageName:@"playbutton.png"];
    
    [self AutomicPlay];
    [self CreatePositionLabel2];
}
#pragma mark 当视图已经出现的时候 创建一个Audio对象 加载音乐
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 如果当前的image 为 播放按钮  那么直接在视图加载完毕的时候 进行播放音乐
    if ([_currentImage isEqual:@"playbutton.png"])
    {
        [self createStreamer];
        [[AudioPlayer ShowInstance].downloadSourceField resignFirstResponder];
        [self setImageName:@"loadingbutton.png"];
        
        [[AudioPlayer ShowInstance].streamer start];
        [self.eq start];
        
    }
    
    
}
// 自动切换下一首 加一个观察者 观察silder的value值.
- (void)AutomicPlay
{
    [self addObserver:self forKeyPath:@"progressSlider.value" options:NSKeyValueObservingOptionNew context:@"gaibian"];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.titleOne.text = [[self.playArray objectAtIndex:self.index] title];
    
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
    if ((([AudioPlayer ShowInstance].streamer.duration -[AudioPlayer ShowInstance].streamer.progress) < 6) && (([AudioPlayer ShowInstance].streamer.duration -[AudioPlayer ShowInstance].streamer.progress)  > 2 ))  {
        
        [self destroyStreamer];
        if ([[self.playArray lastObject] title] == [[self.playArray objectAtIndex:self.index] title]) {
            
        }
        else{
            self.index+=1;
            
            if (self.Titlestr != nil) {
                self.titleOne.text = [[self.playArray objectAtIndex:self.index] title];
            }
            if (self.imagePath != nil) {
                [[AudioPlayer ShowInstance].imageView1 sd_setImageWithURL:[NSURL URLWithString:[[self.playArray objectAtIndex:self.index] coverLarge]]];
            }
            
            
            if (self.mp3Path != nil) {
                [AudioPlayer ShowInstance].downloadSourceField.text = [[self.playArray objectAtIndex:self.index] playUrl64];
            }
            [self createStreamer];
            [[AudioPlayer ShowInstance].downloadSourceField resignFirstResponder];
            //            [self setImageName:@"loadingbutton.png"];
            [[AudioPlayer ShowInstance].streamer start];
        }
    }
    
    
}


#pragma mark 单例给imageView赋值
- (void)toopImage
{
    [AudioPlayer ShowInstance].imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds
                                                                                          .size.height / 1.6675)];
    
    
    if (self.imagePath != nil) {
        [[AudioPlayer ShowInstance].imageView1 sd_setImageWithURL:[NSURL URLWithString:self.imagePath] placeholderImage:[UIImage imageNamed:@"sleep.png"]];
    }
}
#pragma mark 给当前的image赋值 以便作为是否播放的条件
- (void)setImageName:(NSString *)imageName
{
    if (!imageName)
    {
        imageName = @"playButton.png";
    }
    self.currentImage = imageName;
    
    UIImage *image = [UIImage imageNamed:imageName];
    [self.playView.layer removeAllAnimations];
    [self.playView setImage:image];
    if ([imageName isEqual:@"loadingbutton.png"])
    {
        [self spinButton];
    }
    
    
}

- (void)spinButton
{
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
    CGRect frame = [self.playView frame];
    self.playView.layer.anchorPoint = CGPointMake(0.5, 0.5);
    self.playView.layer.position = CGPointMake(frame.origin.x + 0.5 * frame.size.width, frame.origin.y + 0.5 * frame.size.height);
    [CATransaction commit];
    
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanFalse forKey:kCATransactionDisableActions];
    [CATransaction setValue:[NSNumber numberWithFloat:2.0] forKey:kCATransactionAnimationDuration];
    CABasicAnimation *animation;
    animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = [NSNumber numberWithFloat:0.0];
    animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
    animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
    animation.delegate = self;
    [self.playView.layer addAnimation:animation forKey:@"rotationAnimation"];
    [CATransaction commit];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)finished
{
    if (finished)
    {
        [self spinButton];
    }
    [NSThread sleepForTimeInterval:1.2];
    [self setImageName:@"stopbutton.png"];
}




//设置歌曲链接的
- (void)CreatedownloadSourceField
{
    [AudioPlayer ShowInstance].downloadSourceField = [[UITextField alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height + 10, 200, 80)];
    [AudioPlayer ShowInstance].downloadSourceField.backgroundColor = [UIColor cyanColor];
    if (self.mp3Path != nil) {
        
        [AudioPlayer ShowInstance].downloadSourceField.text = [[self.playArray objectAtIndex:self.index] playUrl64];
    }
    
    [self.view addSubview:[AudioPlayer ShowInstance].downloadSourceField];
}

// 创建音频对象
- (void)createStreamer
{
    if ([AudioPlayer ShowInstance].streamer)
    {
        return;
    }
    
    [self destroyStreamer];
    self.escapedValue = [AudioPlayer ShowInstance].downloadSourceField.text;
    
    NSURL *url = [NSURL URLWithString:self.escapedValue];
    [AudioPlayer ShowInstance].streamer = [[AudioStreamer alloc] initWithURL:url];
    self.progressUpdateTimer =
    [NSTimer
     scheduledTimerWithTimeInterval:0.1
     target:self
     selector:@selector(updateProgress:)
     userInfo:nil
     repeats:YES];
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(playbackStateChanged:)
     name:ASStatusChangedNotification
     object:[AudioPlayer ShowInstance].streamer];
}



// 进度条创建
- (void)CreateProgressSlider
{
    if (self.view.frame.size.width == 414) {
        _progressSlider = [[UISlider alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 4.41, self.view.bounds.size.height / 1.36, self.view.bounds.size.width / 1.875, 40)];
    }else{
        _progressSlider = [[UISlider alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 4.41, self.view.bounds.size.height / 1.36, self.view.bounds.size.width / 1.875, 40)];}
    _progressSlider.minimumValue = 1;
    _progressSlider.maximumValue = 100;
    _progressSlider.backgroundColor = [UIColor blackColor];
    _progressSlider.alpha = 0.6;
    [_progressSlider addTarget:self action:@selector(Monitor:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_progressSlider];
    
    
}

//显示歌曲进度的label
- (void)CreatePositionLabel
{
    if (self.view.frame.size.width == 320) {
        self.positionLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 12.8 , self.view.bounds.size.height / 1.36, self.view.bounds.size.width / 6.25, 40)];
    }else if(self.view.bounds.size.width == 414){
        self.positionLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 11.19, self.view.frame.size.height / 1.36, self.view.bounds.size.width / 6.25, 40)];
        
    }else{
        
        self.positionLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 10.71, self.view.bounds.size.height / 1.36, self.view.bounds.size.width / 6.25, 40)];
    }    self.positionLabel.userInteractionEnabled = YES;
    self.positionLabel.backgroundColor = [UIColor clearColor];
    //    _positionLabel.alpha = 0.4;
    self.positionLabel.font = [UIFont systemFontOfSize:15.3];
    self.positionLabel.textAlignment = NSTextAlignmentLeft;
    _positionLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:self.positionLabel];
}

- (void)updateProgress:(NSTimer *)updatedTimer
{
    
    
    if ([AudioPlayer ShowInstance].streamer.bitRate != 0.0)
    {
        double progress = [AudioPlayer ShowInstance].streamer.progress;
        double duration = [AudioPlayer ShowInstance].streamer.duration;
        if (duration > 0)
        {
            NSInteger proTime = (NSInteger)progress;
            
            NSInteger m = proTime % 60;
            NSString *tempM = [NSString stringWithFormat:@"%ld", (long)m];
            if (m < 10) {
                tempM = [NSString stringWithFormat:@"0%ld", (long)m];
            }
            
            
            NSString *currentTime = [NSString stringWithFormat:@"%ld:%@", proTime / 60, tempM];
            
            
            NSInteger endtime = (NSInteger)duration;
            NSInteger P = (endtime - 6) % 60;
            NSString *PM = [NSString stringWithFormat:@"%ld", (long)P];
            if (P < 10) {
                PM = [NSString stringWithFormat:@"0%ld", (long)P];
            }
            
            
            NSInteger endTime = (NSInteger)duration ;
            NSString *currentEndTime = [NSString stringWithFormat:@"%ld:%@", endTime / 60, PM];
            
            [_positionLabel setText:
             [NSString stringWithFormat:@"%@",currentTime]];
            
            [_positionLabel2 setText:[NSString stringWithFormat:@"%@",currentEndTime]];
            
            [_progressSlider setEnabled:YES];
            [_progressSlider setValue:100 * progress / duration];
        }
        else
        {
            [_progressSlider setEnabled:NO];
        }
    }
    else
    {
        _positionLabel.text = @"";
    }
    
}

- (void)playbackStateChanged:(NSNotification *)aNotification
{
    if ([[AudioPlayer ShowInstance].streamer isWaiting])
    {
        // 这段代码有bug .
        [self setImageName:@"loadingbutton.png"];
        
        
    }
    else if ([[AudioPlayer ShowInstance].streamer isPlaying])
    {
        [self setImageName:@"stopbutton.png"];
        if ([_currentImage isEqualToString:@"loadingbutton.png"]) {
            [self setImageName:@"stopbutton.png"];
        }
    }
    else if ([[AudioPlayer ShowInstance].streamer isIdle])
    {
        [self destroyStreamer];
        
        [self setImageName:@"playbutton.png"];
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)sender
{
    [sender resignFirstResponder];
    [self createStreamer];
    return YES;
}






- (void)backButtonAction:(UIButton *)button{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createView
{
    
    
    
    // 一层透明的View
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width / 2.5)];
    view.backgroundColor = [UIColor blackColor];
    
    view.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
    
    view.alpha = 0.3;
    [_someView addSubview:view];
    
    
    
    
 
    
    // 蓝色圆盘
    self.backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width / 1.8, self.view.bounds.size.width / 1.8)];
    _backView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
    _backView.layer.masksToBounds = YES;
    _backView.layer.cornerRadius = self.view.bounds.size.width / 3.6;
    
    _backView.image = [UIImage imageNamed:@"sleep.jpg"];
    [_someView addSubview:_backView];
    
    self.centerView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width / 2.5,self.view.bounds.size.width / 2.5)];
    
    self.centerView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
    _centerView.userInteractionEnabled = YES;
    _centerView.layer.masksToBounds = YES;
    _centerView.layer.cornerRadius = self.view.bounds.size.width / 5.0;
    [_centerView sd_setImageWithURL:[NSURL URLWithString:_musicPlay.coverLarge] placeholderImage:[UIImage imageNamed:@"sleep.jpg"]];
    // 模拟导航栏
    self.naviView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 64)];
    _naviView.backgroundColor = [UIColor blackColor];
    _naviView.alpha = 0.3;
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _backButton.frame = CGRectMake(10,70,40,30);
    [_backButton setImage:[[UIImage imageNamed:@"back.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    [_someView addSubview:_backButton];
    
    
    
    // 标题
    self.titleOne = [[UILabel alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width, 50)];
    self.titleOne.font = [UIFont systemFontOfSize:16];
    self.titleOne.textColor = [UIColor whiteColor];
    self.titleOne.textAlignment = NSTextAlignmentCenter;
    self.titleOne.text = [[self.playArray objectAtIndex:self.index] title];
    
    
    [_someView addSubview:_naviView];
    
    [_someView addSubview:_titleOne];
    
    [_backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_someView addSubview:_centerView];
    
}


- (void)cratePlayView
{
    self.totalView = [[UIView alloc] initWithFrame:CGRectMake(0, 8.0 / 9 * self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height / 9)];
    [self.someView addSubview:_totalView];
    _totalView.alpha = 0.5;
    _totalView.backgroundColor = [UIColor blackColor];
    
    // 播放键
    self.playView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,40.0 / 375 * self.view.bounds.size.width, 40.0f / 375 * self.view.bounds.size.width)];
    _playView.center = CGPointMake([UIScreen mainScreen].bounds.size.width /  2,[UIScreen mainScreen].bounds.size.height * 17 / 18);
    _playView.tag = 11;
    _playView.image = [UIImage imageNamed:@"playbutton.png"];
    
    [_someView addSubview:_playView];
    
    // 上一首.
    self.frontSong = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,25.0f / 375 * self.view.bounds.size.width, 25.0f / 375 * self.view.bounds.size.width)];
    _frontSong.center = CGPointMake(self.view.bounds.size.width / 3, self.view.bounds.size.height * 17 / 18);
    _frontSong.image = [UIImage imageNamed:@"left2.png"];
    _frontSong.tag = 12;
    [_someView addSubview:_frontSong];
    // 下一首
    self.nextSong = [[UIImageView alloc] initWithFrame:self.frontSong.bounds];
    _nextSong.center = CGPointMake(self.view.bounds.size.width * 2 / 3, self.view.bounds.size.height  * 17 / 18);
    _nextSong.image = [UIImage imageNamed:@"right2.png"];
    _nextSong.tag = 13;
    [_someView addSubview:_nextSong];
    
  
    
    
    
    
    
    // 给imageView开交互
    //    _totalView.userInteractionEnabled = YES;
    _playView.userInteractionEnabled = YES;
    _shareView.userInteractionEnabled = YES;
    _nextSong.userInteractionEnabled = YES;
    _frontSong.userInteractionEnabled = YES;
    //    self.view.userInteractionEnabled = YES;
    self.someView.userInteractionEnabled = YES;
    // 加手势 播放
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionOne:)];
    [_playView addGestureRecognizer:tap];
    
    
    // 上一首
    UITapGestureRecognizer *tapfrontSong = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionFrongAction:)];
    [_frontSong addGestureRecognizer:tapfrontSong];
    
    // 下一首
    
    UITapGestureRecognizer *tapNext = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionNextSong:)];
    [_nextSong addGestureRecognizer:tapNext];
    
    // 分享
    
    UITapGestureRecognizer *shareTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareTapAction:)];
    [_shareView addGestureRecognizer:shareTap];
    
    //    NSLog(@"%f",_nextSong.center.y);
    
}

- (void)Monitor:(UISlider *)slider
{
    if ([_currentImage isEqualToString:@"playbutton.png"]) {
        [self CreatedownloadSourceField];
        [self createStreamer];
        [[AudioPlayer ShowInstance].streamer start];
    }
    double newSeekTime = (_progressSlider.value / 100.0) * [AudioPlayer ShowInstance].streamer.duration;
    [[AudioPlayer ShowInstance].streamer seekToTime:newSeekTime];
}

- (void)sliderMoved:(UISlider *)aSlider
{
    if ([AudioPlayer ShowInstance].streamer.duration)
    {
        
        double newSeekTime = (aSlider.value / 100.0) * [AudioPlayer ShowInstance].streamer.duration;
        [[AudioPlayer ShowInstance].streamer seekToTime:newSeekTime];
    }
}


- (void)createAnimation
{
    CABasicAnimation *basicAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    basicAnim.repeatCount = _music.duration * 10;
    basicAnim.fromValue = @(0);
    basicAnim.toValue = @(2 * M_PI);
    basicAnim.duration = 10;
    basicAnim.speed = 0.2;
    [basicAnim setAutoreverses:NO];
    [_centerView.layer addAnimation:basicAnim forKey:@"123"];
}

// 播放
- (void)tapActionOne:(UITapGestureRecognizer*)tap
{
    if (!self.flag && [_currentImage isEqualToString:@"stopbutton.png"]) {
        [UIView animateWithDuration:3 animations:^{
            _centerView.center = CGPointMake(self.view.bounds.size.width *  2, -100);
        }];
    }else{
        [UIView animateWithDuration:2 animations:^{
            _centerView.center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
            
        }];
    }
    self.flag = !_flag;
    
    if ([_currentImage isEqual:@"playbutton.png"])
    {
        
        [self createStreamer];
        [[AudioPlayer ShowInstance].downloadSourceField resignFirstResponder];
        [self setImageName:@"loadingbutton.png"];
        [[AudioPlayer ShowInstance].streamer start];
        [self.eq start];
        
    }else if ([_currentImage isEqual:@"stopbutton.png"])
    {
        [[AudioPlayer ShowInstance].streamer pause];
        [self setImageName:@"playbutton.png"];
        [self.eq stop];
    }
    
    
    
    
}
// 上一首
- (void)tapActionFrongAction:(UITapGestureRecognizer *)tap
{
    if (_currentImage)
    {
        self.index-=1;
        if (self.index == -1) {
            self.index = -1 + 1;
            self.alertView = [[UIAlertView alloc] initWithTitle:nil message:@"已经是第一首歌了" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [self.alertView show];
            [self.view addSubview:self.alertView];
            //            NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(alertViewHidden:) userInfo:nil repeats:YES];
            //            NSRunLoop *runloop = [NSRunLoop currentRunLoop];
            //            [runloop addTimer:timer forMode:NSDefaultRunLoopMode];
            //            [timer fire];
            
        }
        else{
            [self destroyStreamer];
            if (self.Titlestr != nil) {
                self.titleOne.text = [[self.playArray objectAtIndex:self.index] title];
            }
            
            else
            {
                if (self.imagePath != nil) {
                    
                    [[AudioPlayer ShowInstance].imageView1 sd_setImageWithURL:[NSURL URLWithString:[[self.playArray objectAtIndex:self.index] coverLarge]]];
                }
                
                
            }
            
            
            
            if (self.mp3Path != nil) {
                [AudioPlayer ShowInstance].downloadSourceField.text = [[self.playArray objectAtIndex:self.index] playUrl64];
            }
            
            
        }
        [self createStreamer];
        [[AudioPlayer ShowInstance].downloadSourceField resignFirstResponder];
        [self setImageName:@"stopbutton.png"];
        [[AudioPlayer ShowInstance].streamer start];
    }
}

// 下一首
- (void)tapActionNextSong:(UITapGestureRecognizer *)tap
{
    if (_currentImage)
    {
        self.titleOne.text = [[self.playArray objectAtIndex:self.index] title];
        
        //[self destroyStreamer];
        if (self.playArray.count == 1)
        {
            self.alertView = [[UIAlertView alloc] initWithTitle:nil message:@"已经是最后一首歌了" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [self.alertView show];
            [self.view addSubview:self.alertView];
            if (self.Titlestr != nil)
            {
                self.titleOne.text = [[self.playArray objectAtIndex:self.index] title];
                
            }
            if (self.imagePath != nil)
            {
                [[AudioPlayer ShowInstance].imageView1 sd_setImageWithURL:[NSURL URLWithString:[[self.playArray objectAtIndex:self.index] coverLarge]]];
            }
            
            if (self.mp3Path != nil)
            {
                [AudioPlayer ShowInstance].downloadSourceField.text = [[self.playArray objectAtIndex:self.index] playUrl64];
            }
            // NSLog(@"%@",[AudioPlayer ShowInstance].downloadSourceField.text);
            [self createStreamer];
            [[AudioPlayer ShowInstance].downloadSourceField resignFirstResponder];
            [self setImageName:@"stopbutton.png"];
            
            [[AudioPlayer ShowInstance].streamer start];
            
        }
        if (self.playArray.count != 1) {
            if ([[self.playArray lastObject] title] == [[self.playArray objectAtIndex:self.index] title]) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"已经是最后一首歌了" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                [alertView show];
                [self.view addSubview:alertView];
                
            }
            else{
                [self destroyStreamer];
                self.index+=1;
            }
            
            
            
            
            
            
            
            
            if (self.mp3Path != nil)
            {
                [AudioPlayer ShowInstance].downloadSourceField.text = [[self.playArray objectAtIndex:self.index] playUrl64];
            }
            
            [self createStreamer];
            [[AudioPlayer ShowInstance].downloadSourceField resignFirstResponder];
            [self setImageName:@"stopbutton.png"];
            [[AudioPlayer ShowInstance].streamer start];
            
            
            
        }
        
    }
    
}

// 分享

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"musicName" object:self.mp3Path];
}
- (void)destroyStreamer
{
    if ([AudioPlayer ShowInstance].streamer)
    {
        [[NSNotificationCenter defaultCenter]
         removeObserver:self
         name:ASStatusChangedNotification
         object:[AudioPlayer ShowInstance].streamer];
        self.progressUpdateTimer = nil;
        
        [[AudioPlayer ShowInstance].streamer stop];
        
        [AudioPlayer ShowInstance].streamer = nil;
    }
}
- (void)CreatePositionLabel2
{
    if (self.view.bounds.size.width == 320) {
        self.positionLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size. width / 1.27, self.view.frame.size.height / 1.36, self.view.bounds.size.width / 4.6, 40)];
        self.positionLabel2.backgroundColor = [UIColor blueColor];
    }else if(self.view.bounds.size.width == 414){
        self.positionLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size. width / 1.27, self.view.bounds.size.height / 1.36, self.view.bounds.size.width / 4.6, 40)];
        
        
    }else{
        
        self.positionLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size. width / 1.27, self.view.bounds.size.height / 1.36, self.view.bounds.size.width / 4.6, 40)];
        self.positionLabel2.backgroundColor = [UIColor redColor];
    }
    [self.view addSubview:self.positionLabel2];
    // self.positionLabel2.userInteractionEnabled = YES;
    self.positionLabel2.backgroundColor = [UIColor clearColor];
    //    _positionLabel2.alpha = 0.5;
    self.positionLabel2.font = [UIFont systemFontOfSize:15.3];
    self.positionLabel2.textAlignment = NSTextAlignmentLeft;
    _positionLabel2.textColor = [UIColor whiteColor];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
